package top.kiterunner.learning.concurrency_test.ReentrantLockDemo;

// ReentrantLock用于替代synchronized

import java.util.concurrent.TimeUnit;

/**
 * AdorynZhao in Tencent-UESTC.
 * Source Code Created on 2017/7/7.
 */
public class ReentrantLockDemo1 {

    public synchronized void method1() {
        for (int i = 0; i < 10; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName() + " sleep " + i + "th second.");
        }
    }

    public synchronized void method2() {
        System.out.println(Thread.currentThread().getName() + " execute method2.");
    }

    public static void main(String[] args) {
        ReentrantLockDemo1 rld1 = new ReentrantLockDemo1();

        new Thread(rld1::method1, "Thread_1").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(rld1::method2, "Thread_2").start();
    }
}
